package com.foreknow.test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.foreknow.entity.ProvinceData;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import com.alibaba.fastjson.JSON;



/**
 * Servlet implementation class FeiyanServlet
 */
@WebServlet("/FeiyanServlet")
public class FeiyanServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
//    public FeiyanServlet() {
//        super();
//        // TODO Auto-generated constructor stub
//    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		response.getWriter().append("Served at: ").append(request.getContextPath());
//		response.getWriter().append(" {\"name\": '����',\"value\": '253' }");
		response.setHeader("content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
//		response.getWriter().println(" {\"name\": \"北京\",\"value\": \"253\" }");

		response.getWriter().println(list2Json());
		System.out.println("打印结果1");

	}


	 // list转Json
//	@Test
    public  String list2Json() {
		List<ProvinceData> list = new ArrayList<ProvinceData>();
		TestData td=	new TestData();
		String content=td.printgetfile("utf-8","feiyandata2.txt");
		String html=content;
		Document doc = Jsoup.parse(html);
		Elements province=  doc.getElementsByClass("prod");
		for(Element e1:province) {
			 ProvinceData proviceinfo=new ProvinceData(
					 e1.getElementsByClass("area").text().toString(),
					 e1.getElementsByClass("confirm").text().toString()
					 );
			list.add(proviceinfo);
		}
        String json = JSON.toJSONString(list, true);
        System.out.println("List集合转json格式字符串 :" + json);
        return json;
    }
	@Test
    public  void list1Json() {
		List<ProvinceData> list = new ArrayList<ProvinceData>();
		TestData td=	new TestData();
		String content=td.printgetfile("utf-8","feiyandata2.txt");
		String html=content;
		Document doc = Jsoup.parse(html);
		Elements province=  doc.getElementsByClass("prod");
		for(Element e1:province) {
			 ProvinceData proviceinfo=new ProvinceData(
					 e1.getElementsByClass("area").text().toString(),
					 e1.getElementsByClass("confirm").text().toString()
					 );
			list.add(proviceinfo);
		}
        String json = JSON.toJSONString(list, true);
        System.out.println("List集合转json格式字符串 :" + json);

    }
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		doGet(request, response);

	}

}
